package com.atguigu.scala2.chapter5

import org.junit.Test

/**
 * @author lizikuan
 * @date 2021/7/12 20:38
 */
class Test1 {
  def add(x:Int,y:Int):Int = {
    val z = x+y
    z
  }


  @Test
  def test1() = {
    val x = 10
    val y = 20
    val result = add(x,y)
    println(result)
  }

    //可变参数
  @Test
  def test2()={
    val ints = List(1, 2, 3, 4, 6)
    println(sum(ints: _*))
    println(sum(1,2,3,4,5,6,7,8))
  }

  def sum(x:Int*): Int ={
    val sum1 = x.sum
    sum1
  }

  @Test
  def test3(): Unit ={

    val add1 = (x:Int,y:Int) => {
      x+y
    }

    val add2 = (x:Int,y:Int) => x-y

    val hello = ()=>{
      print("hello world")
    }
    val add3 = add _
    println(add1(1, 2))
    println(add2(3,1))
    println(add3(1,1))
    hello()
  }


  def myAdd(x:Int,y:Int,func:(Int,Int)=>Int) ={
    val i = func(x, y)
    i
  }

  @Test
  def test4 (): Unit = {
    val func = (x:Int,y:Int) => {
      x+y
    }
    println(myAdd(10,20,func))
    println(myAdd(11,11,(x,y)=>x+y))
    println(myAdd(10,2,_+_))

  }

  /**
   * 1、根据指定规则对集合中每个元素操作
   *     数据: Array("spark","hadoop","flume","kafka")
   *     规则: 获取每个元素的长度 [可变]
   *     结果: Array(5,6,5,5)
   */
  @Test
  def test5 = {
    val array = Array("spark", "hadoop", "flume", "kafka")
    val array1 = array.map(_.length)
    println(array1.toList)
  }


  /**
   * 2、根据指定规则对数据进行过滤
   *     数据: Array(10,2,4,5,7,9,20)
   *     规则: 过滤掉奇数数据 [可变]
   *     结果: Array(10,2,4,20)
   */
  @Test
  def test6() = {
    val array = Array(10, 2, 4, 5, 7, 9, 20)
    val array1 = array.filter(_ % 2 == 0)
    println(array1.toList)
  }


  /**
   * 3、根据指定规则获取集合中的最大元素
   *     数据: Array("zhangsan 20 3000","lisi 15 2500","zhaoliu 30 4000")
   *     规则: 获取年龄最大的人信息 [可变]
   *     结果: zhaoliu 30 4000
   *
   */
  @Test
  def test7(): Unit = {
    val array = Array("zhangsan 50 3000", "lisi 15 2500", "zhaoliu 30 4000")
    val func = (x:String) => {
      val strings = x.split(" ")
      strings(1).toInt
    }
    println(myMax(array, func))
  }

  def myMax(datas:Array[String],func :String=>Int) ={
    var maxData = datas(0)
    var maxAge = func(datas(0))
    for ( i <- 1 until  datas.length){
      if(maxAge < func(datas(i))){
        maxAge = func(datas(i))
        maxData = datas(i)
      }
    }
    maxData
  }

  /**
   * 4、根据指定规则对集合中的元素进行分组
   * 数据: Array("zhangsan 男 shenzhen","lisi 女 beijing","zhaoliu 男 beijing")
   * 规则: 根据地址分组 [可变]
   * 结果: Map(shenzhen->List( "zhangsan 男 shenzhen" ), biejing->List( "lisi 女 beijing","zhaoliu 男 beijing") )
   *
   */
  @Test
  def test8(): Unit = {

  }

}
